// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { show } = require("../../game/alert");
const { shopData } = require("../gameData");

cc.Class({
    extends: cc.Component,

    properties: {
        oneMonth: cc.Button,
        threeMonth: cc.Button,
        sixMonth: cc.Button,
        monthLabel: cc.Label,
        moneyPercent: cc.Slider,
        monthTips:cc.Label,
        moneyTips:cc.Label,
        payMoney: cc.Button,
        rentMoney: cc.Button,
        curMoney:1000,
        money:10000,
        interest:0,
    },

    // LIFE-CYCLE CALLBACKS:
    onEnable(){
        if(shop.debt.money > 0){
            this.oneMonth.interactable = false;
            this.threeMonth.interactable = false;
            this.sixMonth.interactable = false;
            this.money = shop.debt.money;
            this.monthLabel.string = `${shop.debt.month}个月`;
            this.monthTips.string = `${shop.debt.month}个月后偿还本金，每个月利息${formatMoney(shop.debt.interest)}`;
            this.moneyPercent.process = 0;
            this.moneyTips.string = `${formatMoney(0)}元 / ${formatMoney(shop.debt.money)}元`
        }else{
            this.money = (shop.startMoney + shop.income*10) * (Math.ceil(parseInt(1) / 3) % 3);//能贷款金额
            this.month = 1;
            this.monthLabel.string = `1个月`;
            this.monthTips.string = `1个月后偿还本金，每个月利息??`;

            this.moneyPercent.process = 0;
            this.moneyTips.string = `${formatMoney(0)}元 / ${formatMoney(this.money)}元`
            this.payMoney.interactable = false;
        }
    },
    // onLoad () {},
    changeActive() {
        this.node.active = !this.node.active;
    },
    onSlider(target,i){
        let percent = target.progress;
        this.percent = percent;
        if(shop.debt.money>0){
            this.monthTips.string = `请先还清贷款`;
            this.moneyTips.string = `${formatMoney(shop.debt.money * percent)}元 / ${formatMoney(shop.debt.money)}元`

            return ;
        }
        this.curMoney = percent * this.money;
        this.moneyTips.string = `${formatMoney(this.curMoney)}元 / ${formatMoney(this.money)}元`
        this.monthTips.string = `${this.monthLabel.string}个月后偿还本金，每个月利息${formatMoney(this.interest * this.curMoney/100)}`;
    },
    changeMonth(target, i){
        this.money = (shop.startMoney + shop.income*10) * (Math.ceil(parseInt(i) / 3) % 3);//能贷款金额
        console.log(this.money)
        this.monthLabel.string = `${i}个月`;
        this.month = i;

        if(i == 6){
            this.interest = 18; //月利息
        }else{
            this.interest = 24 - (i - 1) * 2; //月利息
        }
        this.monthTips.string = `${i}个月后偿还本金，每个月利利率${this.interest}%`;
        this.moneyTips.string = `${formatMoney(this.curMoney)}元 / ${formatMoney(this.money)}元`

    },
    ensureRent() {
        // 计算欠款
        shop.debt = {
            money: this.curMoney,
            interest: this.interest * this.curMoney/100,
            month: this.month,
            rate: this.interest,
        }
        console.log(this.rentMoney)
        //更新还能借多少
        this.rentMoney.interactable = false;
        this.oneMonth.interactable = false;
        this.threeMonth.interactable = false;
        this.payMoney.interactable = true;
        this.sixMonth.interactable = false;
        this.moneyTips.string = `${formatMoney(this.curMoney * this.moneyPercent.progress)}元 / ${formatMoney(this.curMoney)}元`;
        shop.money += this.curMoney;
        console.log(shop.money)
        show('未来银行','借款成功');
    },
    payDebt(){
        if(shop.money >= shop.debt.money * this.percent){
        //
        shop.money -= shop.debt.money * this.percent;
        shop.debt.money -= shop.debt.money * this.percent;
        shop.debt.interest -= this.interest * shop.debt.money/100;
        if(shop.debt.money > 0){
            
            show('未来银行','部分还款成功');
            return ;
        }
        this.payMoney.interactable = false;
        this.rentMoney.interactable = true;
        this.oneMonth.interactable = true;
        this.threeMonth.interactable = true;
        this.sixMonth.interactable = true;
        shop.debt = {
                money: 0,
                interset: 0,
                month: 0,
                rate: 0,
        }
        show('未来银行','贷款还清啦');

        }else{
            //余额不足
            show('未来银行','余额不足')
        }
    },
    

    // update (dt) {},
});
